define(["jquery","bbcare",
        "scprit/department/vm/assessmentScreening/temperamentAssessment.ui.js",
        "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"],
    function ($,$bb,$ui,$servers) {
        var event = {};
        event.dataSheet = function(data){
            var dt = [];
            var i = 0;
            for(var item in data){
                ++i;
                dt.push({"id":item ,'ques':data[item],"index":i});
            }
            return dt;
        };
        event.dataDeal =  function(data,id){
            if(!id){
                for(var i = 0;i<data.length;i++){
                    if(data[i].index <= 7){
                        data[i].showFlag = true;
                    }
                }
            }else{
                var index;
                for(var i = 0;i<data.length;i++){
                    if(data[i].id == id){
                        index = data[i].index;
                        break;
                    }
                }
                for(var i = 0;i<data.length;i++){
                    if(index <= 7){
                        if(data[i].index <= 7){
                            data[i].showFlag = true;
                        }else{
                            data[i].showFlag = false;
                        }
                    }else if(index > data.length - 7){
                        if(data[i].index > data.length - 7){
                            data[i].showFlag = true;
                        }else{
                            data[i].showFlag = false;
                        }
                    }else{
                        if(data[i].index >= index && data[i].index < index+7){
                            data[i].showFlag = true;
                        }else{
                            data[i].showFlag = false;
                        }
                    }
                }
            }
            return data;
        };
        event.scoreCheck = function(data,scoreObj){
            var _this = this;
            $(".ques-options").on("mousedown","li.options",function(){
                var that = this;
                _this.optionsDown(data,scoreObj,that);
            });
            $(".ques-options").on("mouseup","li.options",function(){
                var that = this;
                _this.optionsUp(data,scoreObj,that);
            });
        };
        event.optionsDown = function(data,scoreObj,dom){
            $(dom).addClass("checked").siblings().removeClass("checked");
            var score = $(dom).attr("value");
            var quesId = $("#question").find('ul[class*="ques-checked"]').attr("id");
            var isStart = hasZero();
            if(isStart){
                scoreObj[quesId*1+1] = score.toString();
            }else{
                scoreObj[quesId] = score.toString();
            }
            var idArr = toArr(scoreObj);
            if(idArr.length == data.length){
                var tpl = {
                    "title":"提示",
                    "tips":"所有题目已经完成",
                    "btn1":"提交",
                    "btn0":"返回修改"
                };
                var callback1 = function(){
                    $("#topbar").find("li").eq(0).click();
                };
                var callback0 = function(){
                    var isStart = hasZero();
                    if(isStart){quesId = quesId*1+1}
                    delete scoreObj[quesId];
                    $(dom).removeClass("checked").siblings().removeClass("checked");
                };
                event.dialog(tpl,callback1,callback0);
            }
        };
        event.optionsUp = function(data,scoreObj,dom){
            var quesId = $("#question").find('ul[class*="ques-checked"]').attr("id");
            $("#question").find('ul[id="'+(quesId*1+1)+'"]').addClass("ques-checked").siblings().removeClass("ques-checked");
            $("#subject").find('li[quesId="'+quesId+'"]').addClass("done").removeClass("checking");
            $("#subject").find('li[quesId="'+(quesId*1+1)+'"]').addClass("checking");
            $(dom).removeClass("checked").siblings().removeClass("checked");
            quesShow(quesId,data);
        };
        event.submitScore =  function(data,scoreObj,type,userId){
            var submissionResult =  function(){
                var idArr = toArr(scoreObj);
                if(data.length > idArr.length){
                    var notDo = notDoArr(scoreObj,data);
                    $bb.toastShow('问卷第'+notDo.join("、")+'未做，不可以提交',1500);
                }else{
                    //提交分数：
                    var getScoreFn = function(result){
                        $bb.openPage('temperamentAssessmentResult',{
                            "type":type,
                            "userId":userId
                        });
                    };
                    var scoreParams = $servers.scoreParams(type,{map:scoreObj},'',userId);
                    $servers.getScore(scoreParams,getScoreFn);
                }
            };
            var topbars = [
                {
                    "icon":"fa fa-print",
                    "name":"提交结果",
                    "callback":submissionResult
                }
            ];
            $bb.toolbar(topbars);
        };
        //题目点击事件：
        event.quesClick = function(){
            $("#question").off("click","ul.ques-item");
            $("#question").on("click","ul.ques-item",function(){
                var value = $(this).attr("value"),quesId = $(this).attr('id');
                $(this).addClass("ques-checked").siblings().removeClass("ques-checked");
                $(".ques-options").find('li.options[value="'+value+'"]').addClass("checked").siblings().removeClass("checked");
                $("#subject").find('li[quesId="'+quesId+'"]').addClass("checking").siblings().removeClass("checking");
            });
        };
        //题目完成情况点击事件：
        event.subjectClick = function(data){
            var _this = this;
            $("#subject").off("click","li");
            $("#subject").on("click","li",function(){
                var quesId = $(this).attr('quesId');
                $(this).addClass("checking").siblings().removeClass("checking");
                if($('ul.ques-item[id="'+quesId+'"]').hasClass("ques-hide")){
                    data =_this.dataDeal(data,quesId);
                    $ui.temperamentInit({"id":"#question","data":data});
                }
                $('ul.ques-item[id="'+quesId+'"]').addClass("ques-checked").click().siblings().removeClass("ques-checked");
            });
        };
        //题目id是否含有0：
        var hasZero = function(){
            var isStart = false;
            if($("#question").find('ul[id="0"]').length>0){
                isStart = true;
            }
            return isStart;
        };
        var toArr = function(scoreObj){
            var idArr = [];
            for(var item in scoreObj){
                idArr.push(item);
            }
            return idArr;
        };
        var notDoArr = function(scoreObj,data){
            var idArr = [];
            for(var i = 0;i<data.length;i++){
                idArr.push(data[i].id);
            }
            var isStart = false;
            if($.inArray("0",idArr) >= 0){
                isStart = true;
            }
            var notDo = [];
            for(var i = 0;i<data.length;i++){
                if(isStart){
                    if(!scoreObj[data[i].id*1+1]){
                        notDo.push(data[i].id*1+1);
                    }
                }else{
                    if(!scoreObj[data[i].id]){
                        notDo.push(data[i].id);
                    }
                }
            }
            return  notDo;
        };
        var quesShow = function(id,data){
            var isStart = hasZero();
            var number = 7;
            if(isStart){number = 8;}
            var length = data.length;
            if(id <= length - number){
                $("#question").find('ul[id="'+(id*1)+'"]').addClass("ques-hide");
                $("#question").find('ul[id="'+(id*1+7)+'"]').removeClass("ques-hide");
            }
        };
        event.historyDeal = function(data,scoreObj){
            var idArr = [];
            for(var i = 0;i<data.length;i++){
                idArr.push(data[i].id);
            }
            var isStart = false;
            if($.inArray("0",idArr) >= 0){
                isStart = true;
            }
            for(var i = 0;i<data.length;i++){
                if(isStart){
                    data[i].value = scoreObj[data[i].id*1+1];
                }else{
                    data[i].value = scoreObj[data[i].id*1];
                }
            }
            return data;
        };
        event.viewResult = function(userId,answerId,type){
            var viewResult =  function(){
                $bb.openPage("temperamentAssessmentResult",{
                    "userId":userId,
                    "answerId":answerId,
                    "type":type
                });
            };
            var topbars = [
                {
                    "icon":"fa fa-eye",
                    "name":"查看结果",
                    "callback":viewResult
                }
            ];
            $bb.toolbar(topbars);
        };
        /*
         * 弹窗事件：
         * tpl:渲染弹窗的字段；
         * callback1 :按钮的flag==1 的回调函数；
         * callback0 :按钮的flag==0 的回调函数；
         * params:其他参数
         * */
        event.dialog = function(tplDialog,callback1,callback0,params){
            $ui.dialogInit({"id":"#modal","data":tplDialog});
            $("#modal").off("click",".modal-footer>button");
            $("#modal").on("click",".modal-footer>button",function(){
                var flag = $(this).attr("flag");
                if(flag == "1"){
                    if(callback1 && typeof callback1 == 'function'){
                        params ? callback1(params):callback1();
                    }
                }else if(flag == "0"){
                    if(callback0 && typeof callback0 == 'function'){
                        params ? callback0(params):callback0();
                    }
                }
            });
            $('#myModal').modal({backdrop: "static",keyboard:false,show:true});//点击背景空白处不会关闭；按下键盘esc不会被关闭
        };
        return event;
    });